package com.quectel.core.module.property.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.property.dao.PropertyInfoDao;
import com.quectel.core.module.property.dto.PropertyInfoDto;
import com.quectel.core.module.property.entity.PropertyInfoEntity;
import com.quectel.core.module.property.service.PropertyInfoService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-23 09:09:52
 */
@DubboService
public class PropertyInfoServiceImpl implements PropertyInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PropertyInfoServiceImpl.class);

    private static final String PROPERTY_INFO_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "PROPERTY_INFO_CACHE_BY_ID:";
    private static final String PROPERTY_INFO_CACHE_BY_VILLAGE_ID = RedisCacheConstants.PROJECT_NAME + "PROPERTY_INFO_CACHE_BY_VILLAGE_ID:";

    @Autowired
    private PropertyInfoDao propertyInfoDao;
    @Autowired
    private VillageService villageService;


    @Override
    public PropertyInfoDto selectCacheByVillageId(Long villageId) {
        return CacheKit.cacheToRedis(
                () -> {
                    PropertyInfoEntity entity = propertyInfoDao.selectOne(new LambdaQueryWrapper<PropertyInfoEntity>()
                            .eq(PropertyInfoEntity::getVillageId, villageId).last(SqlConstants.MYSQL_LIMIT_ONE));
                    return CopyUtils.copyObj(entity, PropertyInfoDto.class);
                },
                PROPERTY_INFO_CACHE_BY_VILLAGE_ID + villageId,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PropertyInfoDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PROPERTY_INFO_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PropertyInfoDto selectById(Long id) {
        PropertyInfoEntity entity = propertyInfoDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, PropertyInfoDto.class));
    }

    @Override
    public List<PropertyInfoDto> queryList(Map<String, Object> params) {
        List<PropertyInfoEntity> list = propertyInfoDao.queryList(params);
        List<PropertyInfoDto> result = CopyUtils.copyList(list, PropertyInfoDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private PropertyInfoDto paddingField(PropertyInfoDto dto) {
        if (dto != null) {
            VillageDto villageDto = villageService.selectCacheById(dto.getVillageId());
            if (villageDto != null) {
                dto.setVillageName(villageDto.getName());
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return propertyInfoDao.queryTotal(params);
    }

    @Override
    public Long save(PropertyInfoDto dto) {
        PropertyInfoEntity entity = CopyUtils.copyObj(dto, PropertyInfoEntity.class);
        entity.setId(Snowflake.nextId());
        propertyInfoDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PropertyInfoDto dto) {
        PropertyInfoEntity entity = CopyUtils.copyObj(dto, PropertyInfoEntity.class);
        propertyInfoDao.updateById(entity);
        CacheKit.invalidRedisCache(PROPERTY_INFO_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(PropertyInfoDto dto) {
        PropertyInfoEntity entity = CopyUtils.copyObj(dto, PropertyInfoEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, propertyInfoDao);

        CacheKit.invalidRedisCache(PROPERTY_INFO_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        propertyInfoDao.deleteById(id);

        CacheKit.invalidRedisCache(PROPERTY_INFO_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
